package com.miao.Mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author <a href="https://gitee.com/a-tom-is-cry">Xing</a>
 * @Date 2025/7/24 21:54
 * @Description 使用装饰器模式，对baseMapper进行封装，现在可以直接返回DTO对象了
 */
public class DtoMapperDecorator<T, D> {

    private final BaseMapper<T> baseMapper;
    private final EntityDtoConverter<T, D> converter;

    public DtoMapperDecorator(BaseMapper<T> baseMapper,
                              EntityDtoConverter<T, D> converter) {
        this.baseMapper = baseMapper;
        this.converter = converter;
    }

    // =============== 返回 DTO 的查询方法 ===============
    public D selectById(Serializable id) {
        T entity = baseMapper.selectById(id);
        return entity != null ? converter.toDto(entity) : null;
    }

    public List<D> selectBatchIds(Collection<? extends Serializable> idList) {
        List<T> entities = baseMapper.selectBatchIds(idList);
        return entities.stream()
                .map(converter::toDto)
                .collect(Collectors.toList());
    }

    public List<D> selectByMap(Map<String, Object> columnMap) {
        List<T> entities = baseMapper.selectByMap(columnMap);
        return entities.stream()
                .map(converter::toDto)
                .collect(Collectors.toList());
    }

    public D selectOne(Wrapper<T> queryWrapper) {
        T entity = baseMapper.selectOne(queryWrapper);
        return entity != null ? converter.toDto(entity) : null;
    }

    public List<D> selectList(Wrapper<T> queryWrapper) {
        List<T> entities = baseMapper.selectList(queryWrapper);
        return entities.stream()
                .map(converter::toDto)
                .collect(Collectors.toList());
    }

    // =============== 直接委托给原始 Mapper 的方法 ===============
    public Integer insert(T entity) {
        return baseMapper.insert(entity);
    }

    public Integer deleteById(Serializable id) {
        return baseMapper.deleteById(id);
    }

    public Integer updateById(T entity) {
        return baseMapper.updateById(entity);
    }

    // =============== 添加对分页查询的支持 ===============
//    public PageData<D> selectPage(PageData<T> page, Wrapper<T> queryWrapper) {
//        PageData<T> entityPage = baseMapper.selectPage(page, queryWrapper);
//
//        PageData<D> dtoPage = new PageData<>();
//        dtoPage.setTotal(entityPage.getTotal());
//        dtoPage.setSize(entityPage.getSize());
//        dtoPage.setCurrent(entityPage.getCurrent());
//        dtoPage.setPages(entityPage.getPages());
//
//        dtoPage.setRecords(entityPage.getRecords().stream()
//                .map(converter::toDto)
//                .collect(Collectors.toList()));
//
//        return dtoPage;
//    }

    // =============== 提供原始 Mapper 的访问 ===============
    public BaseMapper<T> getOriginalMapper() {
        return baseMapper;
    }
}